<?php
namespace App\Http\Middleware;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Closure;
class Authenticate{


	public function handle(Request $request, Closure $next){

		if(Auth::guard()->guest() && !$this->shouldPassThrough($request)){

			if (Auth::guard()->guest()) {
            	return redirect()->guest(admin_base_path('auth/login'));
        	}
		}	
		
        return $next($request);
	}	

	 /**
     * Determine if the request has a URI that should pass through verification.
     *
     * @param \Illuminate\Http\Request $request
     *
     * @return bool
     */
    protected function shouldPassThrough($request)
    {
        $excepts = [
            admin_base_path('auth/login'),
            admin_base_path('auth/logout'),
        ];

        foreach ($excepts as $except) {
            if ($except !== '/') {
                $except = trim($except, '/');
            }

            if ($request->is($except)) {
                return true;
            }
        }

        return false;
    }	
}